//
//  HWHomeNormalController.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/16.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

import JXSegmentedView
class HWHomeNormalController: HWBaseViewController {
    var tableView:UITableView!
    var tableViewProtocol = HWHomeNormalProtocol()
    var httpManager = HWHomeNormalHttpManager()
    var normalModel:HWHomeNormalModel = HWHomeNormalModel()
    var type:String = "" {
        didSet {
            tableViewProtocol.type = type
        }
    }
    var gameType:String = "0"
    var selectTypeIndex:Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
    }
    
}

extension HWHomeNormalController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWHomeNormalController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWHomeNormalController {
    func loadData() -> Void {
        tableView.mj_footer?.endRefreshing()
        httpManager.getHomeNormalInfo(type: self.type, gameType:self.gameType) { model in
            self.tableView.mj_header?.endRefreshing()
            self.normalModel = model
            self.normalModel.selectIndex = self.selectTypeIndex
            self.tableViewProtocol.model = self.normalModel
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        httpManager.getHomeNormalInfoFooter(type: self.type, gameType: self.gameType) { model in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if (model.allGames?.games?.count ?? 0) > 0 {
                for mod in (model.allGames?.games)! {
                    self.normalModel.allGames?.games?.append(mod)
                }
            } else {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            }
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.showHUD(title: error)
        }
        
    }
    
    
}

extension HWHomeNormalController {
    func addAction() -> Void {
        self.tableViewProtocol.clickTagView = {
            typeId,clickIndex in
            self.gameType = typeId
            self.selectTypeIndex = clickIndex
            self.loadData()
        }
        
        self.tableViewProtocol.didClickGame = {
            let gameDetail = HWGameDetailController()
            gameDetail.gameId = $0
            self.navigationController?.pushViewController(gameDetail, animated: true)
        }
    }
}
